package com.xquant.marketdata.marketdata.controller;

import com.xquant.common.core.domain.Underlying;
import com.xquant.marketdata.marketdata.domain.InstrumentPrice;
import com.xquant.marketdata.marketdata.service.InstrumentService;
import com.xquant.marketdata.marketdata.service.impl.DailyPriceServiceImpl;
import com.xquant.marketdata.utils.ResponseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * @Auther: B946
 * @Date: 23/12/2019 10:16
 * @Description:
 */
@RestController
@RequestMapping("instrument")
public class InstrumentController {


    @Autowired
    private InstrumentService instrumentService;


    @Autowired
    private DailyPriceServiceImpl dailyPriceService;

    @RequestMapping(value = "getdailyprice.action")
    public ResponseVo getDailyPriceService(@RequestParam Map<String, Object> param) {
        InstrumentPrice marketPrice = dailyPriceService.getMarketPrice(param);
        return ResponseVo.success(marketPrice);
    }

    @RequestMapping(value = "remoteGetdailypriceByBegDate.action")
    public Map remoteGetdailypriceByBegDate(@RequestParam Map<String, Object> param) {
        Map<String, Object> marketPrice = dailyPriceService.getMarketPriceByBegDate(param);
        return marketPrice;
    }

    @RequestMapping(value = "remoteGetdailyprice.action")
    public InstrumentPrice remoteGetDailyPriceService(@RequestParam Map<String, Object> param) {
        InstrumentPrice marketPrice = dailyPriceService.getMarketPrice(param);
        return marketPrice;
    }

    @RequestMapping(value = "/selectUnderlyingDatas.action")
    public Object selectUnderlyingDatas(@RequestParam Map<String, Object> param) {
        List<Underlying> list = instrumentService.selectUnderlyingDatas(param);
        return list;
    }

    @RequestMapping(value = "/selectUnderlying.action")
    public Object selectUnderlying(@RequestParam Map<String, Object> params) {
        List<Underlying> list = instrumentService.selectUnderlyingDatas(params);
        return list;
    }


    @RequestMapping("/getInstruments.action")
    public Object getInstruments(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        List<Underlying> list = instrumentService.selectUnderlyingDatas(params);
        return list;
    }

    @RequestMapping("/getAllInstrument.action")
    public Object getAllInstrument() {
        List<Map<String, Object>> ret = instrumentService.getAllInstrument();
        return ret;
    }

    @RequestMapping("/getInstrumentsBySubject.action")
    public Object getInstrumentsBySubject(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("condition"));
        String code = request.getParameter("code");
        if (code != null) {
            String[] codes = code.split(",");
            params.put("iCode", codes[0]);
            params.put("aType", codes[1]);
            params.put("mType", codes[2]);
        }
        params.put("start", 0);
        String aTypes = request.getParameter("aType");
        params.put("aTypes", aTypes);
        if (StringUtils.isNotBlank(aTypes)) {
            String[] atypes = aTypes.split(",");
            params.put("aTypes", atypes);
        }
        String end = request.getParameter("limit");
        params.put("end", StringUtils.isBlank(end) ? "25" : end);
        List<Map<String, Object>> list = instrumentService.getInstrumentsBySubject(params);
        return list;
    }

    @RequestMapping("/getInstruments2.action")
    public Object getInstruments2(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("condition"));
        String code = request.getParameter("code");
        if (code != null) {
            String[] codes = code.split(",");
            params.put("iCode", codes[0]);
            params.put("aType", codes[1]);
            params.put("mType", codes[2]);
        }
        params.put("start", 0);
        String aTypes = request.getParameter("aType");
        params.put("aTypes", aTypes);
        if (StringUtils.isNotBlank(aTypes)) {
            String[] atypes = aTypes.split(",");
            params.put("aTypes", atypes);
        }
        String end = request.getParameter("limit");
        params.put("end", StringUtils.isBlank(end) ? "25" : end);

        return instrumentService.getInstruments2(params);
    }

    @RequestMapping("/getIrs.action")
    public Object getIrs(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("condition"));
        params.put("code", request.getParameter("code"));
        params.put("start", 0);
        String aType = request.getParameter("aType");
        params.put("aType", request.getParameter("aType"));
        if (StringUtils.isNotBlank(aType)) {
            String[] atypes = aType.split(",");
            params.put("aType", atypes);
        }
        String end = request.getParameter("limit");
        params.put("end", StringUtils.isBlank(end) ? "25" : end);

        return instrumentService.getIrs(params);
    }

    @RequestMapping("/getBondFutures.action")
    public Object getBondFutures(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("condition"));
        params.put("code", request.getParameter("code"));
        params.put("start", 0);
        String aType = request.getParameter("aType");
        params.put("aType", request.getParameter("aType"));
        if (StringUtils.isNotBlank(aType)) {
            String[] atypes = aType.split(",");
            params.put("aType", atypes);
        }
        String end = request.getParameter("limit");
        params.put("end", StringUtils.isBlank(end) ? "25" : end);

        return instrumentService.getBondFutures(params);
    }

    @RequestMapping("/getBondFutureCtds.action")
    public Object getBondFutureSets(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("condition"));
        params.put("code", request.getParameter("code"));
        params.put("mtrDate", request.getParameter("mtrDate"));
        params.put("start", 0);
        String aType = request.getParameter("aType");
        params.put("aType", request.getParameter("aType"));
        if (StringUtils.isNotBlank(aType)) {
            String[] atypes = aType.split(",");
            params.put("aType", atypes);
        }
        String end = request.getParameter("limit");
        params.put("end", StringUtils.isBlank(end) ? "25" : end);

        return instrumentService.getBondFutureCtds(params);
    }

    @RequestMapping("/getBonds.action")
    public Object getBondSets(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("condition"));
        params.put("code", request.getParameter("code"));
        params.put("futureCode", request.getParameter("futureCode"));
        params.put("start", 0);
        String aType = request.getParameter("aType");
        params.put("aType", request.getParameter("aType"));
        if (StringUtils.isNotBlank(aType)) {
            String[] atypes = aType.split(",");
            params.put("aType", atypes);
        }
        String bCouponType = request.getParameter("bCouponType");
        params.put("bCouponType", bCouponType);
        String end = request.getParameter("limit");
        params.put("end", StringUtils.isBlank(end) ? "25" : end);

        return instrumentService.getBonds(params);
    }

    @RequestMapping("/getCrmws.action")
    public Object getCrmws(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("condition"));
        params.put("code", request.getParameter("code"));
        params.put("start", 0);
        String end = request.getParameter("limit");
        params.put("end", StringUtils.isBlank(end) ? "25" : end);

        return instrumentService.getCrmws(params);
    }

    @RequestMapping("/getFx.action")
    public Object getFx(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("condition"));
        params.put("code", request.getParameter("code"));
        params.put("start", 0);
        String end = request.getParameter("limit");
        params.put("end", StringUtils.isBlank(end) ? "25" : end);

        return instrumentService.getFx(params);
    }

}
